<?php

/*
 *   Blade 2015-01-09
 */

class StaticApp extends MallbaseApp
{
    public function __construct()
    {
        header("Location:".WAP_SITE_URL);
        return;
        parent::__construct();
    }
    /* 恋物志 专题系列 */
    function lianwuzhi()
    {
        $id = $_GET['id'] ? intval($_GET['id']) : 1;
        switch($id)
        {
            case '1':
                $init_data = array(
                        "title" => "【恋物志】反“清”复“明”中西家饰大混战-万菱家饰网wljiashi.com",
                        "keywords" => "恋物志、创意家居饰品、广州家居饰品、家居饰品网、家饰",
                        "description" => "【恋物志】反“清”复“明”中西家饰大混战，上万菱家饰网，挑选你最喜欢的创意家居饰品。");
                $view = 'static.lianwuzhi1.html';
            default:
                # code...
                break;
        }
        $this->assign('init_data', $init_data);
        $this->display($view);
    }

    /* 饰·游记 专题系列 */

    function shiyouji()
    {
        $id = $_GET['id'] ? intval($_GET['id']) : 1;
        switch($id)
        {
            case '1':
                $init_data = array(
                        "title" => "【饰·游记】现代大话西游 之 饰途觅真经-万菱家饰网wljiashi.com",
                        "keywords" => "饰·游记、创意家居饰品、广州家居饰品、家居饰品网、家饰",
                        "description" => "【饰·游记】现代大话西游 之 饰途觅真经，上万菱家饰网，挑选你最喜欢的创意家居饰品。");
                $view = 'static.shiyouji1.html';
            default:
                # code...
                break;
        }
        $this->assign('init_data', $init_data);
        $this->display($view);
    }

    /* 模板固定 */

    function view()
    {
        $id = $_GET['id'] ? intval($_GET['id']) : 1;
        $this->assign('init_data', $this->_get_data($id));
        $this->display('static.view.html');
    }

    /* 纯静态 */

    function index()
    {
        $id = $_GET['id'] ? intval($_GET['id']) : 1;
        switch($id)
        {
            case '1':
                $init_data = array(
                        "title" => "新春特色-花瓶推荐",
                        "keywords" => "家居饰品，家居装饰品，家居用品批发，小商品批发，家居饰品网，家居饰品加盟，广州小商品批发市场",
                        "description" => "万菱家饰网是专业的家居饰品网上商城，家饰行业的实体商家可以进行网上开店，主营家居饰品及家居用品批发和零售，汇聚广州、东莞、深圳等华南地区的家居饰品品牌，是最大最全的家饰网，期待并欢迎家居饰品商户的加盟代理。");
                $view = 'static.flower_vase.html';
                break;
            default:
                # code...
                break;
        }
        $this->assign('init_data', $init_data);
        $this->display($view);
    }

    function _get_data($cate_id)
    {

        $category_mod = & m('specialcategory');
        $cacheServer = cache_server();
        $cacheKey = "static_get_data_" . $cate_id;
        $data = $cacheServer->hGet($category_mod->_name, $cacheKey);

        if($data === false)
        {
            $category = $this->_get_category();
            $category_list = $category['structured'];
            $categorys = $category['category'];
            $goods_list = $this->_get_goods();
            if($category_list)
            {
                foreach($category_list as $key => $val)
                {
                    if($val['id'] == $cate_id)
                    {
                        $category_data = $category_list[$key];
                    }
                }
                $category_data['keywords'] = $categorys[$category_data['id']]['keywords'];
                $category_data['description'] = $categorys[$category_data['id']]['description'];
                $category_data['links'] = $categorys[$category_data['id']]['links'];
                $category_data['goods'] = $goods_list[$category_data['id']];
                if(is_array($category_data['children']) && $category_data['children'])
                {
                    foreach($category_data['children'] as $k => $v)
                    {
                        $category_data['children'][$k]['links'] = $categorys[$v['id']]['links'];
                        $category_data['children'][$k]['goods'] = $goods_list[$v['id']];
                        if(is_array($v['children']) && $v['children'])
                        {
                            foreach($v['children'] as $id => $children)
                            {
                                $category_data['children'][$k]['children'][$id]['links'] = $categorys[$children['id']]['links'];
                                $category_data['children'][$k]['children'][$id]['goods'] = $goods_list[$children['id']];
                            }
                        }
                    }
                }
            }
            $data = $category_data;
            $cacheServer->hSet($category_mod->_name, $cacheKey, $data, $category_mod->expire);
        }

        return $data;
    }

    function _get_category()
    {
        $category_mod = & m('specialcategory');
        $categories = $category_mod->get_list(-1, true);
        $data['category'] = $category_mod->find();
        import('tree.lib');
        $tree = new Tree();
        $tree->setTree($categories, 'cate_id', 'parent_id', 'cate_name');
        $data['structured'] = $tree->getArrayList(0);
        return $data;
    }

    function _get_goods()
    {
        $arr = array();
        $category_mod = & m('specialcategory');
        $sql = "select c.cate_id,g.goods_id,g.goods_name,g.default_image,g.price
                from " . DB_PREFIX . "special_category_goods c
                left join " . DB_PREFIX . "goods g on c.goods_id=g.goods_id";
        $goods_list = $category_mod->getAll($sql);
        if($goods_list)
        {
            foreach($goods_list as $key => $val)
            {
                $arr[$val['cate_id']][$val['goods_id']] = $val;
            }
        }
        return $arr;
    }

}

?>